#!/bin/bash

# @author: lsq
# @date: 2021/06/20

function isUserExist(){
    if id -u $name >/dev/null 2>&1; then
        echo "user $name exists"
        return 1
    else
        echo "user $name does not exist"
        return 0
    fi
}

if [ $# != 1 ];then
    echo "Usage: bash user_add.sh userlist"
elif [ -f $1 ];then
    for line in `cat $1`
    do
        name=`echo $line | cut -d"|" -f1`
        echo "created user: $name"
        # if user exist, then continue
        isUserExist || continue
        password=`echo $line | cut -d"|" -f2`
        useradd ${name}
        echo ${password} | passwd --stdin $name
        chage -d 0 -M 90 -W 7 -I 2 $name
    done
else
    echo "input file not found!"
fi
